﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DreamBox.Admin.User
{
    public partial class GroupCompetence : BLL.BasePage
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DAL.PSelect ps = new DAL.PSelect();
                DataTable dt = ps.Select("dbox_UserCompetence","*","");
                DataTable gDt = ps.Select("dbox_UserGroupCompetence","*","GID="+Request.QueryString["GID"]);
                BLL.Admin.GetGroupCompetenceTree tree = new BLL.Admin.GetGroupCompetenceTree();
                tree.GetTreeList(dt, gDt, treeCompetence);

                treeCompetence.CollapseAll();
                
            }
        }
        //更新权限
        protected void btnPost_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["GID"] != null)
            {
                DAL.Admin.UserGroupCompetence com = new DAL.Admin.UserGroupCompetence();
                Model.Admin.UserGroupCompetence m = new Model.Admin.UserGroupCompetence();
                m.GID = int.Parse(Request.QueryString["GID"]);
                //删除原有权限
                com.Delete(m.GID);
                //添加现有权限数据
                foreach (TreeNode root in treeCompetence.Nodes)
                {
                    if (root.Checked)
                    {
                        m.ComID = int.Parse(root.Value);
                        com.Add(m);
                        AddGroupCompetence(com, m, root);
                    }
                }
                BLL.Admin.Tips.DetailedTips(this, "权限更新成功", "/User/GroupList.aspx", "1");
            }

        }
        private void AddGroupCompetence(DAL.Admin.UserGroupCompetence com,Model.Admin.UserGroupCompetence m,TreeNode node)
        {
            //添加现有权限数据
            foreach (TreeNode child in node.ChildNodes)
            {
                if (child.Checked)
                {
                    m.ComID = int.Parse(child.Value);
                    com.Add(m);
                    //循环子节点
                    AddGroupCompetence(com, m, child);
                }
            }
        }
        //选择节点
        protected void treeCompetence_SelectedNodeChanged(object sender, EventArgs e)
        {
            BLL.Admin.GetGroupCompetenceTree tree = new BLL.Admin.GetGroupCompetenceTree();
            TreeNode node = treeCompetence.SelectedNode;
            if (node.Checked == true)
            {
                node.Checked = false;
            }
            else
            {
                node.Checked = true;
            }
            tree.TreeNodeCheck(node);
          
        }
    }
}